var Project = {
    tableId: "projectTable"
};

function log(msg) {
    setTimeout(() => {
        throw new Error(msg);
    }, 0);
}

var numberPage = 1;

layui.use(['table', 'admin', 'ax', 'form', 'layer', 'jquery','func'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var form = layui.form;
    var layer = layui.layer;
    var func = layui.func;

    /* leez */
    var windowClientWidth = document.body.clientWidth
    var windowClientWidthTemp = document.body.clientWidth
    var tableIsReloading = false

    function _reloadTable() {
        if(tableIsReloading) return
        tableIsReloading = true
        windowClientWidth = document.body.clientWidth;
        if(Math.abs(windowClientWidth - windowClientWidthTemp) > 20) {
            windowClientWidthTemp = windowClientWidth
        }
        setTimeout(() => {
            tableIsReloading = false
        }, 1500)
    }

    /**
     * 初始化表格的列
     */
    Project.initColumn = function () {
        return [[
            // {type: 'checkbox'},
            {type: 'numbers', title: '序号', align: "center", width: 50, minWidth: '40', hide: false,},
            // {field: 'province', sort: false, title: '省', width: 80, align: "left"},
            {field: 'name', sort: false, title: '店名', minWidth: '170', align: "left",templet(d){
                    return '<div style="color: #999">'+d.province+'</div>' + '<div>'+d.name+'</div>';
                }},
            {field: 'gcsclc', sort: false, title: '商场楼层', minWidth: '70', align: "center"},
            {field: 'gcszlc', sort: false, title: '所在楼层', width: 80, align: "center"},
            {field: 'gcshop', sort: false, title: '店铺性质', width: 100, align: "center",templet(d){
                if (d.gcshop == 0){
                    return '新开商城';
                }else if (d.gcshop == 1){
                    return '已开商城 (' + d.gcyksc + '年)';
                }else if (d.gcshop == 2){
                    return '街边店';
                }else if (d.gcshop == 3){
                    return '原铺重装';
                }
                }},
            {field: 'kfry', sort: false, title: '项目工程对接人', minWidth: '150', align: "center",templet(d){
                if (d.kfry != ''){
                    return d.kfry + '(' + d.kfryTel + ')';
                }else {
                    return '';
                }
                }},
            {field: 'zhaoshang', sort: false, title: '半天妖项目工程对接人', minWidth: '160', align: "center",templet(d){
                if (d.zhaoshang != ''){
                    return d.zhaoshang + '(' + d.zhaoshangTel + ')';
                }else {
                    return '';
                }
                }},
            {field: 'htTime', sort: false, title: '合同预计进厂时间', minWidth: '170', align: "center",templet(d){
                if (d.htTime != '' && d.htTime1 != ''){
                    return d.htTime + ' - ' + d.htTime1;
                }else if (d.htTime != '' && d.htTime1 == ''){
                    return d.htTime;
                }else {
                    return '';
                }
                }},
            {field: 'isJp', sort: false, title: '店铺是否毛坯接铺', width: 130, align: "center",templet(d){
                    if (d.isJp == 0){
                        return '是';
                    }else if (d.isJp == 1 && d.party == 0){
                        return '否 (甲方)';
                    }else if (d.isJp == 1 && d.party == 1){
                        return '否 (半天妖工程部)';
                    }
                }},
            {field: 'jpTime', sort: true, title: '接铺时间', width: 100, align: "center"},
            {field: 'content', sort: true, title: '开工时间', width: 100, align: "center"},
            {field: 'openDate', sort: true, title: '开业时间', width: 100, align: "center"},
            {field: 'createTime', sort: true, title: '添加时间', minWidth: '100', align: "center"},

        ]];
    };

    /**
     * 点击查询按钮
     */
    Project.search = function () {
        var queryData = {};
        queryData['provinceId'] = $("#provinceName").val();
        queryData['cityId'] = $("#cityName").val();
        queryData['simpleName'] = $("#name").val();
        Project.tableCfg.where=queryData;
        table.render(Project.tableCfg);
    };

    /**
     * 检索区域
     */
    var qyAjax = new $ax("/area/getAreaByType");
    qyAjax.set("type", 2);
    var qyResult = qyAjax.start();
    $.each(qyResult, function (index, data) {
        $("#provinceName").append("<option value='" + data.id + "'>" + data.simpleName + "</option>");
    });
    form.render('select');
    form.on('select(provinceName)', function (data) {
        var provinceId = data.value;
        if (provinceId == "") {
            $("#cityName").html("");
            $("#cityName").append("<option value=''>全部</option>");
            form.render('select');
        } else {
            var qyAjax = new $ax("/area/getAreaByPid");
            qyAjax.set("pid", provinceId);
            var qyResult = qyAjax.start();
            $("#cityName").html("");
            $("#cityName").append("<option value=''>全部</option>");
            $.each(qyResult, function (index, data) {
                $("#cityName").append("<option value='" + data.id + "'>" + data.simpleName + "</option>");
            });
            form.render('select');
        }
    });

    Project.tableCfg={
        elem: '#' + Project.tableId,
        url:Feng.ctxPath + '/mouldBoardMsg/listDataGongcheng',
        // even: true,
        page:true,
        height:document.body.offsetHeight,
        cellMinWidth: 0,
        cols: Project.initColumn()
    };
    table.render(Project.tableCfg);
    /**
     * 搜索按钮点击事件
     */
    $('#btnSearch').click(function () {
        Project.search();
    });

    /**
     * 工具条点击事件
     */
    table.on('tool(' + Project.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;
        if (layEvent === 'edit') {
            window.location.href = Feng.ctxPath + '/project/shangwu?id=' + data.id + "&swedit=0&auditbtn=0";
        }
    });


});
